package com.wsc.personalblog.controller;

import com.wsc.personalblog.common.Result;
import com.wsc.personalblog.entity.Role;
import com.wsc.personalblog.entity.Tag;
import com.wsc.personalblog.entity.User;
import com.wsc.personalblog.service.RoleService;
import com.wsc.personalblog.service.TagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/{name}")
    @PreAuthorize("hasRole('BOSS')")
    public Result<List<Role>> getRoleInfoByName(@PathVariable("name") String name) {
        List<Role> RoleList = roleService.getRoleInfoByName(name);
        return Result.successHaveData(RoleList);
    }

    /**
     * 查询全部的角色信息
     * @return
     */
    @PreAuthorize("hasRole('BOSS')")
    @GetMapping
    public Result<List<Role>> getRoleInfo(){
        List<Role> RoleList = roleService.getRoleInfo();
        return Result.successHaveData(RoleList);
    }

    /**
     * 添加角色信息
     * @return
     */
    @PostMapping
    @PreAuthorize("hasRole('BOSS')")
    public Result<Integer> addRole(@RequestBody Role role){
        Integer integer = roleService.addRole(role);
        return Result.successHaveData(integer);
    }

    /**
     * 删除角色信息
     * @return
     */
    @DeleteMapping
    @PreAuthorize("hasRole('BOSS')")
    public Result<String> deleteRole(@RequestParam Integer roleId){
        roleService.deleteRole(roleId);
        return Result.successNoData();
    }

    /**
     * 解绑所有用户
     * @return
     */
    @PostMapping("/unbind")
    @PreAuthorize("hasRole('BOSS')")
    public Result<String> unbindAllUser(@RequestParam Integer roleId){
        roleService.unbindAllUser(roleId);
        return Result.successNoData();
    }



}
